package com.xsp.eduservice.controller;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xsp.eduservice.client.VoClient;
import com.xsp.eduservice.entity.EduCourse;
import com.xsp.eduservice.entity.vo.CourseInfoVo;
import com.xsp.eduservice.entity.vo.CoursePublishVo;
import com.xsp.eduservice.entity.vo.CourseQuery;
import com.xsp.eduservice.service.EduCourseService;
import com.xsp.commonutils.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 课程 前端控制器
 * </p>
 *
 * @author testjava
 * @since 2022-02-11
 */
@RestController
@RequestMapping("/eduservice/course")
public class EduCourseController {
    @Autowired
    EduCourseService eduCourseService;
    @Autowired
    VoClient voClient;

    //条件查询和分页
    //如果使用@RequestBody就需要使用@PostMapping
    @PostMapping("pageCourseCondition/{page}/{limit}")
    public R pageCourseCondition(@PathVariable long page,
                                  @PathVariable long limit,
                                  //required = false 设置为非必须的参数
                                  @RequestBody(required = false) CourseQuery courseQuery){
        //调用查询方法
        Page<EduCourse> coursePage = eduCourseService.pageCourseCondition(page,limit,courseQuery);
        //获取总记录数
        long total = coursePage.getTotal();
        //获取拿到的全部数据
        List<EduCourse> records = coursePage.getRecords();
        //返回数据
        return R.ok().data("total",total).data("list",records);
    }


    //所有课程列表
    @GetMapping("getCourseList")
    public R getCourseList(){
        List<EduCourse> list = eduCourseService.list(null);
        return R.ok().data("courseList",list);
    }
    //确认发布课程的信息
    @GetMapping("getCoursePublish/{id}")
    public R getCoursePublish(@PathVariable String id){
        CoursePublishVo coursePublishVo = eduCourseService.getCoursePublishVo(id);
        return R.ok().data("coursePublish",coursePublishVo);
    }

    //添加课程基本信息
    @PostMapping("addCourseInfo")
    //包含了两张表的内容，需要使用vo封装
    //添加完成后需要用到id，因此需要返回id值
    public R addCourseInfo(@RequestBody CourseInfoVo courseInfoVo){
        String courseId = eduCourseService.saveCourseInfo(courseInfoVo);
        return R.ok().data("courseId",courseId);
    }

    //根据课程id查询课程基本信息
    @GetMapping("getCourseInfo/{courseId}")
    public R getCourseInfo(@PathVariable String courseId){
        CourseInfoVo courseInfoVo = eduCourseService.getCourseInfo(courseId);
        return R.ok().data("courseInfoList",courseInfoVo);
    }

    //修改课程信息
    @PostMapping("updateCourseInfo")
    public R updateCourseInfo(@RequestBody CourseInfoVo courseInfoVo){
        eduCourseService.updateCourseInfo(courseInfoVo);
        return R.ok();
    }

    //删除课程信息(小节,章节,描述,课程,视频)
    @DeleteMapping("deleteCourse/{courseId}")
    public R deleteCourse(@PathVariable String courseId){
        eduCourseService.deleteCourse(courseId);
        return R.ok();
    }

    //课程最终发布(将status从Draft改成Normal)
    @GetMapping("coursePublish/{id}")
    public R coursePublish(@PathVariable String id){
        EduCourse eduCourse = new EduCourse();
        eduCourse.setId(id);
        eduCourse.setStatus("Normal");
        eduCourseService.updateById(eduCourse);
        return R.ok();
    }

}

